//
//  ZHWeChatTabBar.m
//  Doctor
//
//  Created by liuzhishan on 16/1/18.
//  Copyright © 2016年 shanshan. All rights reserved.
//

#import "ZHWeChatTabBar.h"
#import "ZHBarItem.h"
#define ITEMCOUNT 4
#define ITEMGAPPRO 0.05




@interface ZHWeChatTabBar ()

@property (nonatomic,strong) NSMutableArray *viewArray;

@property (nonatomic,assign)  NSInteger judgeIndex;

@end

@implementation ZHWeChatTabBar

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
         self.viewArray = [NSMutableArray array];
         [self addMyTabBarView];
       
        
    }
    
    
    return self;
}


#pragma mark 添加子控件
-(void)addMyTabBarView
{
    
    float itemGap = TABWIGHTPRO*WIDTH*ITEMGAPPRO;
    
    float itemWight = (WIDTH*TABWIGHTPRO - (ITEMCOUNT - 1)*itemGap)/ITEMCOUNT;
    
    float leftDis = itemWight + itemGap;
    
    
    for ( int i = 0;  i < ITEMCOUNT ;  i ++) {
        
        ZHBarItem *barItem = [[ZHBarItem alloc]init];
        barItem.index = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapViewAction:)];
        [barItem addGestureRecognizer:tap];
        
        [self.viewArray addObject:barItem];

        [self addSubview:barItem];
        
        [barItem mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.and.bottom.equalTo(@0);
            make.left.equalTo(@(leftDis * i));
            make.width.equalTo(@(itemWight));
            
            
        }];
        
   
    }
}



#pragma mark 处理点击事件，将以前点击的图片变成normal，然后将处理新点击的图片
-(void)tapViewAction:(UITapGestureRecognizer *)tap
{
    
  /*
   以前 判断 把上一个 颜色变回去，然而 会出现 在滑动式在2 将要到 3 然后 期间点击，会出现bug 因为当2 算了 3还是绿色的。
    ZHBarItem *barItem = self.viewArray[self.judgeIndex];
    barItem.imageView.alpha = 1;
    barItem.imageViewHI.alpha = 0;
*/
   
    for (int i = 0;  i< ITEMCOUNT; i++) {
        
        ZHBarItem * barItem = self.viewArray[i];
        barItem.imageView.alpha = 1;
        barItem.imageViewHI.alpha = 0;

        
    }
    
    
    
    ZHBarItem * barItem = (ZHBarItem*)tap.view;
   // self.judgeIndex = barItem.index;
    barItem.imageView.alpha = 0;
    barItem.imageViewHI.alpha = 1;

    
    
    
    [self.tabBarDele getTabBarIndex:barItem.index];
    
    NSLog(@"%ld",self.judgeIndex);
    
    
}


#pragma  mark 初始化数据
-(void)setImageHighArray:(NSArray *)imageHighArray
{
    _imageHighArray = imageHighArray;
    
    for (int i = 0 ;  i < imageHighArray.count; i++) {
        
        ZHBarItem *barItem = self.viewArray[i];

        barItem.titleLable.text = self.titleArray[i];
        barItem.imageView.image = [UIImage imageNamed:self.imageNormalArray[i]];
        barItem.imageViewHI.image = [UIImage imageNamed:self.imageHighArray[i]];
        
        
        
        if(i == 0)
        {
            self.judgeIndex = 0;
            barItem.imageView.alpha = 0;
            barItem.imageViewHI.alpha = 1;
        }
        
    }
    
    
    
    
}


#pragma mark 处理滑动事件

-(void)sliderOffset:(CGFloat)offsetPro andCurrentIndex:(NSInteger)currenIndex andNextIndex:(NSInteger)nextIndex
{
    NSLog(@"%f  ---  %ld ----- %ld",offsetPro,currenIndex,nextIndex);
    
    if (currenIndex < 0 || currenIndex >= ITEMCOUNT  ||  nextIndex < 0 || nextIndex >= ITEMCOUNT) return;
    
    if ((1 - offsetPro) <= 0.001)   {
        
        self.judgeIndex = currenIndex;
    }
    
    ZHBarItem *currentBarItem = self.viewArray[currenIndex];
    ZHBarItem *nextBarItem = self.viewArray[nextIndex];
    
    
    currentBarItem.imageView.alpha = offsetPro;
    currentBarItem.imageViewHI.alpha = 1.0 - offsetPro;
    
    nextBarItem.imageView.alpha = 1.0 - offsetPro;
    nextBarItem.imageViewHI.alpha =  offsetPro;
    
    
    
}

@end
